from main_graph_pumb import main
import eval
import numpy as np
import time
import pandas as pd
import time


def TestOnline(path, r_list, name_list):
    lines = open(path,'r').readlines()
    linesuse = lines
    d={}

    for i, l in enumerate(linesuse):
        try:
            name = l[:-1]
            if name not in name_list:
                parm_list = l.split('autotrain,')[1].split('_/train')[0].split('clasteracc')[0].split('_,')
                d={}
                for parm in parm_list:
                    k = parm.split('_')[-2]
                    v = float(parm.split('_')[-1])
                    d[k]=v
                
                # r = eval.TestClassifacationKMeans(
                r = eval.TestClassifacationKMeans(
                    embedding=np.load(name+'emb.npy'),
                    label=np.load(name+'lab.npy')
                )
                # r2 = eval.TestClassifacationLogisticRegression(
                #     embedding=np.load(name+'emb.npy'),
                #     label=np.load(name+'lab.npy')
                # )
                print(i,'/', len(linesuse))
                d['acc_kmeans']=r[0]
                # d['acc_val_mean']=r2[0]
                # d['acc_val_std']=r2[1]
                # d['acc_test_mean']=r2[2]
                # d['acc_test_std']=r2[3]
                r_list.append(list(d.values()))
                name_list.append(name)
        except:
            pass
    return d, r_list ,name_list

if __name__ == "__main__":
    # path='acclogpath_Cora.txt'
    # path='acclogpath_citeseer.txt'
    # path='acclogpath_pubmed.txt'
    path='acclogpath_wiki.txt'
    
    
    r_list = []
    name_list = []
    while True:
        
        d, r_list, name_list = TestOnline(path, r_list, name_list)
        if len(d.keys()) > 0:

            d = pd.DataFrame(data=np.array(r_list), index=name_list, columns=list(d.keys()))
            d.to_csv(path+'.csv')
            
        print('add', len(d.keys()))
        print('test {} itme and save to {}'.format(len(r_list), path))
        time.sleep(600)
